<?php
/**
 * Created by PhpStorm.
 * User: Admin
 * Date: 2021/4/21
 * Time: 12:39
 */

namespace app\common\helpers;



class CommonHelper
{
    /**
     * 计算百分比
     * @param $num
     * @param $total
     * @return float|int
     */
    public static function rate($num, $total)
    {
        return empty($total) ? 0 : floatval(round($num * 100 / $total, 2));
    }


    /**
     * @param $start
     * @param $end
     * @param string $type 时间类型 month year day hour minute second
     * @return array
     */
    public static function getStartAndEnd($start, $end, $type = "day", $strtotime = false, $format = 'Y-m-d H:i:s')
    {
        $start = date($format, strtotime($start));
        switch ($type) {
            case "year":
            case "month":
            case "hour":
            case "minute":
            case "second":
            default:
                $end = date($format, strtotime("+ 1 {$type} " . $end) - 1);
                break;
        }
        return [
            $strtotime ? strtotime($start) : $start,
            $strtotime ? strtotime($end) : $end,
        ];
    }


    public static function getArrMonth($start, $end)
    {
        $result = [];
        $int_start = strtotime($start);
        $int_end = strtotime($end);

        do {
            $month = date("Y-m", $int_start);
            $result[] = $month;
            $int_start = strtotime($month . " +1 month");
        } while ($int_start <= $int_end);
        return $result;
    }

    public static function countMonthDay($arr_month)
    {
        $int = 0;
        foreach ($arr_month as $month) {
            $int += date("t", strtotime($month));
        }
        return $int;
    }

    /**
     * 返回开始到结束时间的每日日期数组
     * @param $start
     * @param $end
     * @return array
     */
    public static function getDays($start, $end)
    {
        $result = [];
        $int_start = strtotime($start);
        $int_end = strtotime($end);

        do {
            $month = date("Y-m-d", $int_start);
            $result[] = $month;
            $int_start = strtotime($month . " +1 day");
        } while ($int_start <= $int_end);
        return $result;
    }

    /**
     * 计算平均值
     * @param $total
     * @param $num
     * @return float|int
     */
    public static function avg($total, $num)
    {
        return empty($num) ? 0 : floatval(round($total / $num, 2));
    }

    // 返回执行日期所在周的第一天(周一)日期
    public static function firstOfWeek($date)
    {
        $now = strtotime($date);    //当时的时间戳
        $number = date("w", $now);  //当时是周几
        $number = $number == 0 ? 7 : $number; //如遇周末,将0换成7
        $diff_day = $number - 1; //求到周一差几天
        return date("Y-m-d", $now - ($diff_day * 60 * 60 * 24));
    }

    public static function toInt($num)
    {
        return intval($num);
    }

    public static function toFloat($num)
    {
        return floatval(round($num, 2));
    }

    /**
     * 计算时间
     * @param $start
     * @param $end
     * @return string
     */
    public static function useTime($start, $end)
    {
        $int_start = self::timeToInt($start);
        $int_end = self::timeToInt($end);

        $str = "";
        $dif = $int_end - $int_start;
        if ($dif > 86400) {
            $str .= intval($dif / 86400) . "天";
            $dif = $dif % 86400;
        }

        if ($dif > 3600) {
            $str .= intval($dif / 3600) . "小时";
            $dif = $dif % 3600;
        }
        if ($dif > 60) {
            $str .= intval($dif / 60) . "分";
            $dif = $dif % 60;
        }
        if ($dif) {
            $str .= intval($dif) . "秒";
        }
        return $str;
    }

    /**
     * 时间转时间戳
     * @param $time
     * @return false|int
     */
    public static function timeToInt($time)
    {
        if ($time > 1000000000) {
            return $time;
        } else {
            return strtotime($time);
        }
    }

    public static function isMonth($day)
    {
        if (preg_match('/(\d{4})年(\d{1,2})月(\d{1,2})日/', $day, $out)) {
            return false;
        }
        if (preg_match('/(\d{4})(\d{2})(\d{2})/', $day, $out)) {
            return false;
        }
        if (preg_match('/(\d{4})\.(\d{1,2})\.(\d{1,2})/', $day, $out)) {
            return false;
        }

        if (preg_match('/(\d{4})年(\d{1,2})月/', $day, $out)) {
            return true;
        }
        if (preg_match('/(\d{4})(\d{2})/', $day, $out)) {
            return true;
        } elseif (preg_match('/(\d{4})(\d{1})/', $day, $out)) {
            return true;
        }
        if (preg_match('/(\d{4})\.(\d{1,2})/', $day, $out)) {
            return true;
        }

        return false;
    }


    /**
     * @param $num
     * @return int
     * 判断新增、减少、持平
     */
    public static function isZF($num)
    {
        if ($num > 0) {
            return 1;
        } elseif ($num < 0) {
            return 0;
        } else {
            return 2;
        }
    }

    /**
     * 二维数组单值累加
     * @param $array
     * @param $field
     * @return array
     */
    public static function arrayUp($array, $field, $pre = 0)
    {
        $count = 0;
        foreach ($array as $k => $v) {
            if ($k == 0) {
                $count = $v[$field] + $pre;
            } else {
                $count += $v[$field];
            }
            $array[$k][$field] = $count;
        }
        return $array;
    }


    /**
     * @param $field
     * @param $arr
     * @param string $default
     * @return mixed
     * 获取默认值
     */
    public static function getValue($field, $arr, $default = '')
    {
        return isset($arr[$field]) ? $arr[$field]:"";
    }
}